package com.duoec.dw.lineage.dto;

import com.duoec.dw.lineage.exceptions.LineageAnalyseException;

import java.io.Serializable;
import java.util.Objects;

/**
 * 数据库表坐标：{databaseName}.{tableName}
 *
 * @author xuwenzhen
 */
public class TableCoordinateDto implements Serializable {
    private static final int TABLE_NAME_SIZE = 2;
    private static final String DOT_STR = "\\.";
    private static final char DOT_CHAR = '.';

    public TableCoordinateDto() {
    }

    public TableCoordinateDto(String fullTableName) {
        String[] names = fullTableName.split(DOT_STR);
        if (names.length != TABLE_NAME_SIZE) {
            throw new LineageAnalyseException("表全名错误，必须是{databaseName}.{tableName}格式！");
        }

        this.databaseName = names[0];
        this.tableName = names[1];
    }

    /**
     * 库名
     */
    protected String databaseName;

    /**
     * 表名
     */
    protected String tableName;

    public String getDatabaseName() {
        return databaseName;
    }

    public void setDatabaseName(String databaseName) {
        this.databaseName = databaseName;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    @Override
    public String toString() {
        return databaseName + DOT_CHAR + tableName;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        TableCoordinateDto that = (TableCoordinateDto) o;
        return Objects.equals(databaseName, that.databaseName) &&
                Objects.equals(tableName, that.tableName);
    }

    @Override
    public int hashCode() {
        return Objects.hash(databaseName, tableName);
    }
}
